// 窗口相关的处理
import { reactive, ref, provide, toRefs } from 'vue';
function useWindow ()
{

    let data = reactive({
        windowList:[] as any,
        isSelected:'',
        iframeloading:true
    });

    let elW = 0;
    let elH = 0;
    let elLeft = 0;
    let elTop = 0;


    // 窗口选中
    function handleSelect (item) 
    {
        data.isSelected = item.id;
    }

    // iframe加载监听函数
    function iframeLoad () 
    {
        data.iframeloading = false;
    }

    // 窗口缩放
    function handleZoom (item) 
    {
        data.isSelected = item.id;
        setTimeout(() => 
        {
            let window:any = document.querySelector('.window-active');
            if (window.offsetWidth < document.body.offsetWidth || window.offsetHeight < document.body.offsetHeight - 80) 
            {
                // 全屏展示
                elW = window.offsetWidth;
                elH = window.offsetHeight;
                elLeft = window.offsetLeft;
                elTop = window.offsetTop;
                window.style.width = '100%';
                window.style.height = '100%';
                window.style.left = '0%';
                window.style.top = '0%';
             
            }
            else
            {
                if (elH > 0 || elW > 0 || elLeft > 0 || elTop > 0)
                {
                    window.style.width = elW + 'px';
                    window.style.height = elH + 'px';
                    window.style.left = elLeft + 'px';
                    window.style.top = elTop + 'px';
                    return;
                }
               
                window.style.width = document.body.offsetWidth - 100 + 'px';
                window.style.height = document.body.offsetHeight - 100 + 'px';
            }
        }, 30);
    }

    // 窗口隐藏
    function hideWindow  (item) 
    {
        data.windowList.forEach((value) =>
        {
            if (value.id === item.id)
            {
                value.isHide  = true;
            }
        });
        console.log(data.windowList);
        
    }

    // // 窗口显示
    function showWindow (item)
    {
        data.windowList.forEach((value) => 
        {
            if (value.id === item.id)
            {
                value.isHide  = false;
            }
        });
    }

    // 窗口关闭
    function handleClose (item)
    {
        data.windowList.forEach((value, index) => 
        {
            if (value.id === item.id)
            {
                data.windowList.splice(index, 1);
            }
        });
    }

    // 打开窗口
    function openWindow (item)
    {
        console.log(item);
        
        if (item.type === 'web')
        {
            return window.open(item.link, '_blank');
        }
        data.isSelected = item.id;

        let value = data.windowList.find((i:any) => 
        {
            return item.id === i.id;
        });
        if (value) 
        {
            showWindow(value);
            return;
        }
        item.isHide = false;
        data.windowList.push(item);
    }

    return {
        data,
        methods:{
            openWindow,
            iframeLoad,
            handleSelect,
            handleZoom,
            hideWindow,
            showWindow,
            handleClose
        }
    };

}

export default useWindow;